| Conditions | 3 |
| Total Lines | 52 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /*eslint max-len: ["error", { "code": 150 }]*/ |
||
| 42 | |||
| 43 | startChat() { |
||
| 44 | let that = this; |
||
| 45 | |||
| 46 | socket.on('connect', function() { |
||
| 47 | console.info("Connected to chat"); |
||
| 48 | }); |
||
| 49 | |||
| 50 | socket.on('disconnect', function() { |
||
| 51 | console.info("Disconnected from chat"); |
||
| 52 | }); |
||
| 53 | |||
| 54 | socket.on('chat message', function (conversation) { |
||
| 55 | that.updateConversation(conversation); |
||
| 56 | }); |
||
| 57 | |||
| 58 | socket.on('restore conversation', function (conversation) { |
||
| 59 | that.updateConversation(conversation); |
||
| 60 | }); |
||
| 61 | |||
| 62 | socket.on('clear conversation', function () { |
||
| 63 | that.setState({ |
||
| 64 | conversation: [] |
||
| 65 | }); |
||
| 66 | }); |
||
| 67 | |||
| 68 | socket.on('update users', function (users) { |
||
| 69 | let currentUsers = [], |
||
| 70 | currentConversation = that.state.conversation; |
||
| 71 | |||
| 72 | users.all.map(function (user) { |
||
| 73 | currentUsers.push(<li key={user}>{user}</li>); |
||
| 74 | return true; |
||
| 75 | }); |
||
| 76 | |||
| 77 | if (users.new) { |
||
| 78 | currentConversation.push( |
||
| 79 | <div className="chat-container message" key={users.new}> |
||
| 80 | <p className="newUser center">{ users.new } has joined the conversation.</p> |
||
| 81 | </div> |
||
| 82 | ); |
||
| 83 | } else if (users.left) { |
||
| 84 | currentConversation.push( |
||
| 85 | <div className="chat-container message" key={users.left}> |
||
| 86 | <p className="oldUser center">{ users.left } has left the conversation.</p> |
||
| 87 | </div> |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | that.setState({ |
||
| 92 | users: currentUsers, |
||
| 93 | conversation: currentConversation |
||
| 94 | }); |
||
| 162 |